package User;


import java.sql.*;

public class User implements UserDAO {

    String url = "jdbc:sqlserver://localhost:1433;databaseName=Student_Grade_Management_System;trustServerCertificate=true";
    String user = "ysy";
    String password = "051018";

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }




    @Override
    public void deleteUser(String name) {
        String query = "DELETE FROM 用户 WHERE 账号 = ?";

        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {

            ps.setString(1, name);
            ps.executeUpdate();
            System.out.println("用户删除成功！");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getUser() {
        String sql = "select * from 用户";
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement()) {
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 6. 打印列名
            for (int i = 1; i <= columnCount; i++) {
                System.out.print(metaData.getColumnName(i) + "\t");
            }
            System.out.println(); // 换行

            // 7. 打印数据
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String value = rs.getString(i);
                    System.out.print(value + "\t");
                }
                System.out.println(); // 每行数据输出后换行
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}